package com.bos.controller;

import com.bos.dao.AnggaranDAO;
import com.bos.dao.JumlahSiswaDAO;
import com.bos.dao.PeriodeDAO;
import com.bos.dao.TahunDAO;
import com.bos.domain.Anggaran;
import com.bos.domain.JumlahSiswa;
import com.bos.domain.Periode;
import com.bos.editor.PeriodeEditor;
import com.bos.model.Bos02Request;
import com.bos.model.DanaBosKecamatanRequest;
import com.bos.model.DayaSerapRequest;
import com.bos.report.factory.Bos02Factory;
import com.bos.report.factory.DayaSerapFactory;
import com.bos.util.CalendarUtil;
import com.bos.util.enums.JenjangSekolah;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 5/22/11
 * Time: 10:57 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class ReportContoller {

    private JumlahSiswaDAO jumlahSiswaDAO;
    private AnggaranDAO anggaranDAO;
    private PeriodeDAO periodeDAO;
    private TahunDAO tahunDAO;

    private Bos02Factory bos02Factory;
    private DayaSerapFactory dayaSerapFactory;

    /**
     * ***************************** SETTER GETTER **********************************************************************
     */
    @Autowired
    public void setJumlahSiswaDAO(JumlahSiswaDAO jumlahSiswaDAO) {
        this.jumlahSiswaDAO = jumlahSiswaDAO;
    }

    @Autowired
    public void setAnggaranDAO(AnggaranDAO anggaranDAO) {
        this.anggaranDAO = anggaranDAO;
    }

    @Autowired
    public void setPeriodeDAO(PeriodeDAO periodeDAO) {
        this.periodeDAO = periodeDAO;
    }

    @Autowired
    public void setTahunDAO(TahunDAO tahunDAO) {
        this.tahunDAO = tahunDAO;
    }

    @Autowired
    public void setBos02Factory(Bos02Factory bos02Factory) {
        this.bos02Factory = bos02Factory;
    }

    @Autowired
    public void setDayaSerapFactory(DayaSerapFactory dayaSerapFactory) {
        this.dayaSerapFactory = dayaSerapFactory;
    }

    /**
     * ************** METHOD CONTOLLER **********************************************************************************
     */

    @RequestMapping(value = "/report/bos02/new", method = RequestMethod.GET)
    public String createBos02(ModelMap modelMap) {
        modelMap.addAttribute("bos02Request", new Bos02Request());
        modelMap.addAttribute("jenjangSekolahList", Arrays.asList(JenjangSekolah.values()));
        modelMap.addAttribute("tahunList", tahunDAO.getAll());
        return "report/bos02/create";
    }

    @RequestMapping(value = "/report/bos02/pdf", method = RequestMethod.POST)
    public ModelAndView downloadBos02(ModelAndView modelAndView, @ModelAttribute("dayaSerapRequest") Bos02Request bos02Request) {
        if (bos02Request == null || bos02Request.getJenjangSekolah() == null || bos02Request.getPeriode() == null)
            throw new IllegalArgumentException("A Request is required");

        //Ambil sumber data source
        List<Anggaran> anggarans = anggaranDAO.getByJenjangSekolahPeriode(bos02Request.getJenjangSekolah(), bos02Request.getPeriode());
        List<JumlahSiswa> jumlahSiswas = jumlahSiswaDAO.getByJenjangSekolahPeriode(bos02Request.getJenjangSekolah(), bos02Request.getPeriode());

        //Isikan datasource
        bos02Factory.fillDataSource(bos02Request.getJenjangSekolah(), anggarans, jumlahSiswas);

        //isikan parameter
        bos02Factory.putResponseParameter(Bos02Factory.KEY_KAB_KOTA, "Kabupaten");
        bos02Factory.putResponseParameter(Bos02Factory.KEY_NAMA_KAB, "Aceh Besar");
        bos02Factory.putResponseParameter(Bos02Factory.KEY_TANGGAL, CalendarUtil.getCalendarStringFormat());
        bos02Factory.putResponseParameter(Bos02Factory.KEY_PERIODE, bos02Request.getPeriode().getTriwulan().getBulanAwal() + "-" + bos02Request.getPeriode().getTriwulan().getBulanAkhir() + " " + bos02Request.getPeriode().getTahun().getTahun());

        //siapkan ModelAndView yang diisi parameter
        modelAndView = new ModelAndView("bos02Report", bos02Factory.getResponseParameter());

        return modelAndView;
    }

    @RequestMapping(value = "/report/dayaSerap/new", method = RequestMethod.GET)
    public String createDayaSerap(ModelMap modelMap) {
        modelMap.addAttribute("dayaSerapRequest", new DayaSerapRequest());
        modelMap.addAttribute("dayaSerapRequest", new DayaSerapRequest());
        modelMap.addAttribute("jenjangSekolahList", Arrays.asList(JenjangSekolah.values()));
        modelMap.addAttribute("tahunList", tahunDAO.getAll());
        return "report/dayaSerap/create";
    }

    @RequestMapping(value = "/report/dayaSerap/pdf", method = RequestMethod.POST)
    public ModelAndView downloadDayaSerap(ModelAndView modelAndView, @ModelAttribute("dayaSerapRequest") DayaSerapRequest dayaSerapRequest) {
        if (dayaSerapRequest == null || dayaSerapRequest.getJenjangSekolah() == null || dayaSerapRequest.getPeriode() == null)
            throw new IllegalArgumentException("A Request is required");

        //Ambil sumber data source
        List<Anggaran> anggarans = anggaranDAO.getByJenjangSekolahPeriode(dayaSerapRequest.getJenjangSekolah(), dayaSerapRequest.getPeriode());
        List<JumlahSiswa> jumlahSiswas = jumlahSiswaDAO.getByJenjangSekolahPeriode(dayaSerapRequest.getJenjangSekolah(), dayaSerapRequest.getPeriode());

        //Isikan datasource
        dayaSerapFactory.fillDataSource(dayaSerapRequest.getJenjangSekolah(), anggarans, jumlahSiswas);

        //isikan parameter
        dayaSerapFactory.putResponseParameter(DayaSerapFactory.KEY_KAB_KOTA, "Kabupaten");
        dayaSerapFactory.putResponseParameter(DayaSerapFactory.KEY_NAMA_KAB, "Aceh Besar");
        dayaSerapFactory.putResponseParameter(DayaSerapFactory.KEY_TANGGAL, CalendarUtil.getCalendarStringFormat());
        dayaSerapFactory.putResponseParameter(DayaSerapFactory.KEY_PERIODE, dayaSerapRequest.getPeriode().getTriwulan().getBulanAwal() + "-" + dayaSerapRequest.getPeriode().getTriwulan().getBulanAkhir() + " " + dayaSerapRequest.getPeriode().getTahun().getTahun());

        //siapkan ModelAndView yang diisi parameter
        modelAndView = new ModelAndView("dayaSerapReport", dayaSerapFactory.getResponseParameter());

        return modelAndView;
    }

    @RequestMapping(value = "/report/chart/danaBosKecamatan/new", method = RequestMethod.GET)
    public String createChartDanaBosKecamatan(ModelMap modelMap) {
        modelMap.addAttribute("jenjangSekolahList", Arrays.asList(JenjangSekolah.values()));
        modelMap.addAttribute("tahunList", tahunDAO.getAll());
        modelMap.addAttribute("danaBosKecamatanRequest", new DanaBosKecamatanRequest());

        //nilai untuk global varible chart
        modelMap.addAttribute("chartTitle", "");

        return "report/chart/danaBosKecamatan/create";
    }

    @RequestMapping(value = "/report/chart/danaBosKecamatan/new", method = RequestMethod.POST)
    public String createChartDanaBosKecamatan(ModelMap modelMap, @ModelAttribute("danaBosKecamatanRequest") DanaBosKecamatanRequest danaBosKecamatanRequest) {
        if (danaBosKecamatanRequest == null || danaBosKecamatanRequest.getJenjangSekolah() == null || danaBosKecamatanRequest.getPeriode() == null)
            throw new IllegalArgumentException("A Request is required");

        modelMap.addAttribute("jenjangSekolahList", Arrays.asList(JenjangSekolah.values()));
        modelMap.addAttribute("tahunList", tahunDAO.getAll());
        modelMap.addAttribute("danaBosKecamatanRequest", new DanaBosKecamatanRequest());

        //nilai untuk global varible chart
        modelMap.addAttribute("chartTitle", danaBosKecamatanRequest.getPeriode().getTriwulan().getTriwulan() + " " + danaBosKecamatanRequest.getPeriode().getTahun().getTahun() + " (" + danaBosKecamatanRequest.getJenjangSekolah() + ")");
        modelMap.addAttribute("chartJenjangSekolah", danaBosKecamatanRequest.getJenjangSekolah());
        modelMap.addAttribute("chartPeriode", danaBosKecamatanRequest.getPeriode());

        return "report/chart/danaBosKecamatan/create";
    }

    /**
     * ************** OTHER METHOD **************************************************************************************
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Periode.class, new PeriodeEditor(periodeDAO));
    }
}

